// pages/comment/comment.js
import {addComment} from '../../api/comment'
Page({
  data: {
    commentText: '',
    currentLength: 0,
    rating: 0,
    courseId:null,
    sectionId:null
  },
  onLoad(options){
    this.setData({courseId:options.courseId})
    this.setData({sectionId:options.sectionId})
  },
  onCommentInput(e) {
    this.setData({
      commentText: e.detail.value,
      currentLength: e.detail.value.length
    });
  },

  onStarTap(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      rating: index
    });
  },

  async onPublish() {
    if (this.data.commentText.trim() === '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }

    // 这里可以添加发布评论的逻辑，比如发送到服务器等
    console.log("this.data.commentTex",this.data.commentText)
    const res = await addComment(this.data.sectionId, this.data.commentText,this.data.rating,this.data.courseId)
    wx.navigateBack({
      delta: 1  //返回的页面数，如果 delta 大于现有页面数，则返回到首页。
    })
    // 清空输入框和评分
    this.setData({
      commentText: '',
      currentLength: 0,
      rating: 0
    });

    wx.showToast({
      title: '发布成功',
      icon: 'success'
    });
  }
});